/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.model;

import java.io.Serializable;
import java.security.acl.LastOwnerException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

public class BusRoute{
	private int dbId =0;
	private String routeId;
	private String routeName;
	private String routeDir;
	private String routeDirDesp;
	private List<BusStop> stops; //should be lazily populated.
	private Date scheduleLastPopulated;
	private boolean favorite;
	private boolean refreshed;
	
	public BusRoute(){
		
	}

	public BusRoute(String routeId,String routeName){
		this.routeId=routeId;
		this.routeName = routeName;
	}

	public int getDbId(){
		return this.dbId;
	}
	public void setDbId(int id){
		this.dbId = id;
		
	}
	public String getRouteId() {
		return routeId;
	}


	public void setRouteId(String routeId) {
		this.routeId = routeId;
	}


	public String getRouteName() {
		return routeName;
	}


	public void setRouteName(String routeName) {
		this.routeName = routeName;
	}


	public String getRouteDir() {
		return routeDir;
	}


	public void setRouteDir(String routeDir) {
		this.routeDir = routeDir.toUpperCase();
	}


	public String getRouteDirDesp() {
		return routeDirDesp;
	}


	public void setRouteDirDesp(String routeDirDesp) {
		this.routeDirDesp = routeDirDesp;
	}
	
	


	public List<BusStop> getStops() {
		if (stops==null)
			stops = new ArrayList<BusStop>();
		return stops;
	}


	public void setStops(List<BusStop> stops) {
		this.stops = stops;
	}
	
	

	

	public Date getScheduleLastPopulated() {
		return scheduleLastPopulated;
	}
	
	public boolean needRepopulate(long threshold){
		if(scheduleLastPopulated==null)
			return true;
		long cur= System.currentTimeMillis();
		long last=scheduleLastPopulated.getTime();
		
		if(cur>last+threshold*60*1000){
			return true;
		}
		return false;
	}

	public void setScheduleLastPopulated(Date scheduleLastPopulated) {
		this.scheduleLastPopulated = scheduleLastPopulated;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((routeDir == null) ? 0 : routeDir.hashCode());
		result = prime * result + ((routeId == null) ? 0 : routeId.hashCode());
		return result;
	}
	
	


	public boolean isFavorite() {
		return favorite;
	}

	public void setFavorite(boolean favorite) {
		this.favorite = favorite;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BusRoute other = (BusRoute) obj;
		if((this.dbId!=0)&& (this.dbId == other.dbId))
			return true;
		if (routeDir == null) {
			if (other.routeDir != null)
				return false;
		} else if (!routeDir.equals(other.routeDir))
			return false;
		if (routeId == null) {
			if (other.routeId != null)
				return false;
		} else if (!routeId.equals(other.routeId))
			return false;
		return true;
	}


	@Override
	public String toString() {
		return 
			  (routeName != null ? routeName : "") 
			  +(routeDir != null ? "---" + routeDir:"");
	}

	public boolean isTimeScheduleRefreshed() {
		
		return refreshed;
	}
	
	public void setTimeScheduleRefreshed(boolean refreshed){
		
		this.refreshed = refreshed;
	}


	
		
	

}
